'##################################################
'###  Top Level (Get root Path from system env)
'##################################################
function GetEnv(envVarName)
	On Error Resume Next
	' 创建Shell对象并获取值
	Set WshShell = CreateObject("WScript.Shell")
	envValue = WshShell.Environment("PROCESS")(envVarName) ' 优先查找进程级变量

	' 如果未找到则尝试系统级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("SYSTEM")(envVarName)
	End If

	' 如果仍未找到则尝试用户级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("USER")(envVarName)
	End If

	' 检查结果并输出
	If Err.Number = 0 And envValue <> "" Then
		GetEnv=envValue
	Else
		GetEnv=""
	End If
end function

'##################################################
'##################################################
'##################################################
sub IncludeVbs(filepath)
	Dim fso, file, content
	Set fso = CreateObject("Scripting.FileSystemObject")
	' 读取functions.vbs的内容
	Set file = fso.OpenTextFile(filepath, 1) ' 1表示只读
	content = file.ReadAll
	file.Close
	' 将functions.vbs的内容载入全局作用域
	ExecuteGlobal content
end sub

'##################################################
'##################################################
'##################################################
dim root,tmpfile
Dim doc
Set doc = application.ActiveDocument
main
set doc = nothing
sub main()	
	'// REUSE
	envVarName ="EXP_SHORTCUTS_ROOT"
	root = GetEnv(envVarName)'"C:\MentorGraphics\2005EXP\SDD_HOME\pvtk\env\"
	if root ="" then 
		msgbox "please set env value (EXP_SHORTCUTS_ROOT = ?)"
		exit sub
	end if
	
	call IncludeVbs(root & "\skill_lib.vbs")
	
	If ValidateServer(doc) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If	
	
	tmpfile = doc.path & "output\tmp.vbs"
	'// TODO
	Dim pcbTraces,pcbTrace
	Dim TraceANetname,TraceAWidth,TraceAnumPoints,TraceApntsArr,TraceAanchortype
	Dim TraceBNetname,TraceBWidth,TraceBnumPoints,TraceBpntsArr,TraceBanchortype
	Dim TraceLyr,TraceALyr,TraceBLyr
	Dim i
	i=0
	Dim PasteTraceAObj,PasteTraceBObj
	If application.LockServer Then
		Set pcbTraces = doc.Traces(epcbSelectSelected)
		for each pcbTrace In pcbTraces
		if i = 0 then 
			TraceApntsArr = pcbTrace.Geometry.pointsarray
			TraceAnumPoints = UBound(pcbTrace.Geometry.pointsarray, 2) + 1
			TraceALyr = pcbTrace.Layer
			Set TraceANetname = doc.FindNet(pcbTrace.Net.Name)
			TraceAWidth = pcbTrace.Geometry.LineWidth
			pcbTrace.delete()
		end if
		if i <> 0 then 
			TraceBpntsArr = pcbTrace.Geometry.pointsarray
			TraceBnumPoints = UBound(pcbTrace.Geometry.pointsarray, 2) + 1
			TraceBLyr = pcbTrace.Layer
			Set TraceBNetname = doc.FindNet(pcbTrace.Net.Name)
			TraceBWidth = pcbTrace.Geometry.LineWidth
			pcbTrace.delete()
		end if			      
		i = i+ 1
		next 


		doc.TransactionStart (0)
		Set PasteTraceAObj = doc.PutTrace(TraceALyr, TraceBNetname, TraceAWidth, TraceAnumPoints, TraceApntsArr, Nothing, epcbAnchorNone, epcbUnitCurrent)
		Set PasteTraceBObj = doc.PutTrace(TraceBLyr, TraceANetname, TraceBWidth, TraceBnumPoints, TraceBpntsArr, Nothing, epcbAnchorNone, epcbUnitCurrent)
		doc.TransactionEnd (1)
		application.UnlockServer
	end if
End Sub

